home *** CD-ROM | disk | FTP | other *** search
/ WINMX Assorted Textfiles / Ebooks.tar / Text - Tech - Programming - PHP - Introduction To, (TXT).zip / introduction-to-php.txt
Text File  |  2000-05-09  |  7KB  |  317 lines

  1.        An Introduction to PHP
  2.          by Shaun McCormick
  3.  
  4.  Lesson I: An Intro
  5.  
  6.      PHP, or Hypertext Preprocessor,
  7.  is a embedded server-side scripting
  8.  language. Not only is PHP's
  9.  popularity growing among web
  10.  designers, but its features are
  11.  growing with the release and
  12.  development of PHP4. This article
  13.  will cover the aspect of basic
  14.  programming with PHP3, however,
  15.  since PHP4 is only in the Beta 3
  16.  stage. PHP is embedded straight
  17.  into HTML code, and is
  18.  automatically parsed when the
  19.  viewer loads a document. It is
  20.  mostly used as a module with the
  21.  Apache web server for Linux;
  22.  however, it can easily be used in
  23.  other OS-based servers such as
  24.  Microsoft's IIS or Netscape's
  25.  Enterprise server. PHP can be
  26.  downloaded freely at its homepage,
  27.  www.php.net.
  28.  
  29.      PHP is highly similar to
  30.  Microsoft's ASP (Active Server
  31.  Pages) and Coldfusion. However, PHP
  32.  is free and cross-platform. As a
  33.  combination of C++ and CGI
  34.  techniques, PHP can send HTTP
  35.  headers, cookies, communicate
  36.  flawlessly with databases, and much
  37.  more. Unlike CGI, all PHP requires
  38.  is a installation and your
  39.  PHP-coded files to have the
  40.  extension .php3 (it can also be
  41.  configured to use .php, .phtml, and
  42.  others.). To implement code, one
  43.  would simply start as if you were
  44.  writing a HTML file. To separate
  45.  the coding from the HTML, you place
  46.  the separators:
  47.  <?php
  48.  ?>
  49.  (You can also configure PHP to use
  50.  ASP-style tags (<% %>) or even
  51.  <script LANGUAGE="php"></script>.)
  52.  
  53.  To output from the code, one would
  54.  use the command:
  55.    echo "Hello, $name";
  56.  
  57.  The $name will automatically be
  58.  parsed and printed. For instance,
  59.  if $name was John, it would have
  60.  printed:
  61.    Hello, Shaun.
  62.  print $variable; can also be used,
  63.  as well as printf("Hello, world!");
  64.  
  65.  Since PHP is like C++ and CGI,
  66.  statements MUST end with a
  67.  semicolon, ;
  68.  echo "Hi!"
  69.  Doesn't work, while
  70.  echo "Hi!";
  71.  Does.
  72.  
  73.  Hence, one would create a file
  74.  named hello.php3 and put this code
  75.  in it:
  76.  
  77.  <html><head><title>PHP
  78.  Test</title></head>
  79.  <body>
  80.  <?php
  81.  $name = "John";
  82.  echo "Hello $name\n";
  83.  ?>
  84.  </body></html>
  85.  
  86.  Comments are also used in PHP.
  87.  
  88.  // This is a comment.
  89.  /* So is this! */
  90.  # Well, I am too!
  91.  
  92.  Onto the next lesson!
  93.  
  94.  Lesson II: Usefulness? Sure, why
  95.  not!
  96.  
  97.  Let's start with some interesting
  98.  code.
  99.  
  100.  <?php
  101.  if(strstr($HTTP_USER_AGENT,"MSIE"))
  102.  {
  103.    echo "You are using Internet
  104.  Explorer<br>";
  105.  }
  106.  ?>
  107.  
  108.  Wow, lots of new concepts to grasp
  109.  today. We'll start with the if
  110.  statement. The going for a if
  111.  statement is as follows:
  112.  if ($name == $myname) { run_code; }
  113.  // or...
  114.  if ($name == "John") { run_code; }
  115.  // Else statments can be used too:
  116.  if ($name == "John") { echo "Hi
  117.  John!"; } else { echo "Who are
  118.  you?"; } // Don't forget elseif!
  119.  if ($name == "John") { echo "Hi
  120.  John!"; } elseif ($name == "Suzy")
  121.  { echo "Heya Suzy!"; } else { echo
  122.  "Who are you?"; }
  123.  
  124.  Notice that the == has two equals.
  125.  == is comparative. To see if they
  126.  are unequal, one would use !=
  127.  Just one equals sign, or =, is used
  128.  for assigning values, not
  129.  comparing.
  130.  Now that we've got that out of the
  131.  way, let's move on.
  132.  
  133.  if
  134.  (strstr($HTTP_USER_AGENT,"MSIE")) {
  135.  
  136.  Ok, strstr() is a PHP function that
  137.  checks if one string includes
  138.  another. $HTTP_USER_AGENT is a
  139.  HTTP-sent variable. It is sent by
  140.  the requesting browser of the
  141.  visitor. So if $HTTP_USER_AGENT
  142.  contains MSIE, the code will print
  143.  out:
  144.  You are using Internet Explorer<br>
  145.  Of course, the <br> will be
  146.  interpreted by the server and sent
  147.  out as a newline.
  148.  
  149.  Variables in PHP are dealt with
  150.  very similar to CGI and C++.
  151.  
  152.  $var = 23;
  153.  $othervar = 12;
  154.  $var++;  // This makes $var be 24.
  155.  $var = $var + 2; // $var now is 26.
  156.  It can also be written as $var +=
  157.  2;
  158.  $var = $var + $othervar; // $var is
  159.  now 26 + 12, or 38.
  160.  
  161.  Strings are dealt with a little
  162.  differently.
  163.  
  164.  $firstname = "John";
  165.  $lastname = "Doe";
  166.  $fullname = $firstname . $lastname;
  167.  // A . is used combine strings.
  168.  $firstname .= " " . $lastname //
  169.  Makes $firstname become "John Doe".
  170.  Note the " " to add the space
  171.  between the names.
  172.  echo "Hi, $firstname!\n"; //
  173.  Outputs "Hi, John Doe!" with a
  174.  newline in the code. Newlines are
  175.  used to make code easier to read.
  176.  
  177.  Next lesson!
  178.  
  179.  Lesson III: Loops, loops, and more
  180.  loops!
  181.  
  182.  What is a loop? A loop is simply a
  183.  structure of code that repeats
  184.  itself until a certain point has
  185.  been reached. Let's start with the
  186.  while loop.
  187.  
  188.  echo "Let's count to 10!";
  189.  // There's no need to set $x to 0,
  190.  PHP assumes all unset variables to
  191.  be 0.
  192.  while ($x < 11) {  // < means less
  193.  than, > means more than. You could
  194.  also make this $x != 11
  195.  echo $x . " ";
  196.  $x++;
  197.  }
  198.  This would output: 1 2 3 4 5 6 7 8
  199.  9 10
  200.  Simple, eh? The code can also be
  201.  structured like so:
  202.  do {
  203.  echo $x . " ";
  204.  $x++;
  205.  } while ($x < 11);
  206.  Same exact output, just the while
  207.  statement is checked at the end of
  208.  the code.
  209.  
  210.  For loops are quite similar, and a
  211.  little more complex.
  212.  for($x = 1; $x < 11; $x++) {
  213.  echo $x . " ";
  214.  }
  215.  
  216.  On we go!
  217.  
  218.  Lesson IV: Using Forms
  219.  
  220.  Now that you've heard all this talk
  221.  of variables and loops and the
  222.  such, you're probably asking, "How
  223.  in the heck is this going to help
  224.  me?" Well, I'll show you. Say you
  225.  had a php3 file named demo.php3
  226.  with the following form in it:
  227.  
  228.  <?php
  229.  function displayform() { //
  230.  functions are code that is run only
  231.  when called.
  232.  global $PHP_SELF; // The script
  233.  itself, demo.php3. To make the
  234.  variable accessible in all
  235.  functions, use global.
  236.  ?>
  237.  <FORM METHOD="POST" ACTION="<?php
  238.  echo $PHP_SELF; ?>"> // see how we
  239.  made it act on itself?
  240.  Name: <INPUT TYPE="text"
  241.  NAME="name">
  242.  <input type="submit"
  243.  value="Submit"><input type="reset"
  244.  value="Reset">
  245.  </FORM>
  246.  <?
  247.  }
  248.  function doform() {
  249.  global $name;
  250.  if ($name = "John Smith") { echo
  251.  "Hey John!"; } else { echo "Who are
  252.  you?"; }
  253.  }
  254.  if (empty($name)) {
  255.  displayform(); // If the variable
  256.  wasn't set, make the form reappear.
  257.  } else {
  258.  doform(); // If it was, process the
  259.  form.
  260.  }
  261.  
  262.  Of course, there is many more
  263.  practical usages and ways to code
  264.  this. It's coded simply so you
  265.  could easily understand. Next
  266.  lesson!
  267.  
  268.  Lesson V: Cookies!
  269.  
  270.  Cookies, although often put down,
  271.  are a very powerful part of web
  272.  programming. Let's say you wanted
  273.  to give a user a nickname, but you
  274.  wanted it to store over a long
  275.  period of time. No problem! Here's
  276.  the code for cookie.php3
  277.  
  278.  <?php
  279.  function addcookie() {
  280.  $nick = "Sammy"; // The value of
  281.  the cookie
  282.  $var = "nick"; // The name of the
  283.  cookie
  284.  setcookie($var,$nick,time()+360);
  285.  // add the cookie, with the name
  286.  "nick" and the value "Sammy", and
  287.  make it last 5 minutes.
  288.  Header("Location: cookie.php3"); //
  289.  Send the user back to this script.
  290.  Note: Header must be used BEFORE
  291.  any other output.
  292.  }
  293.  function docookie() {
  294.  $nickname =
  295.  $HTTP_COOKIE_VARS["nick"]; // set
  296.  the value of the cookie "nick" to
  297.  $nickname
  298.  echo " Hi $nickname!";
  299.  }
  300.  if (!$HTTP_COOKIE_VARS["nick"]) {
  301.  // Check to see if the cookie
  302.  "nick" exists, if not, set it. If
  303.  so, execute the code.
  304.  addcookie();
  305.  } else { docookie(); }
  306.  ?>
  307.  
  308.  Easy, huh? That's about it for now,
  309.  look for more advanced stuff coming
  310.  your way soon.
  311.  
  312.        ⌐ 1999 Shaun McCormick
  313.  
  314.   ⌐ 1999 dotcomma. All Rights Reserved.
  315.  
  316.  
  317.